home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / CPPAWARE.PAK / DBMISC.CPP < prev    next >
Text File  |  1997-05-06  |  10KB  |  343 lines

  1. //--------------------------------------------------------
  2. // DBMISC.CPP
  3. // Copyrights (C) 1996 Borland International
  4. // All rights reserved.
  5. //--------------------------------------------------------
  6.  
  7. #define __DDEML_H       // prevent the including DDEML.H in building this module
  8. #define _INC_DDEMLH     // due to a conflict in type declaration of HSZ
  9. #define __DECLARE_HSZ   // still need to force in the correct VB declaration of HSZ
  10.  
  11. #include <bivbx.h>      // included first to get type HSZ declared
  12.  
  13. #include <vdbt/dbmisc.h>
  14.  
  15.  
  16. typedef HCTL (FAR PASCAL _export *LPFNCTLNITROFIND)( HCTL, LPSTR, PIUnknown );
  17.  
  18. //--------------------------------------------------------
  19. // Helper Functions
  20. //--------------------------------------------------------
  21. extern bool DBEqualOperator( TGenericTableEntry __RTFAR& entry
  22.                            , TEventHandler::TEventInfo& info )
  23. {
  24.   return entry.Msg == info.Msg && entry.Pmf != NULL;
  25. }
  26.  
  27. extern void PASCAL ThrowException( LPCSTR lpError )
  28. {
  29.   TXOwl( lpError ).Throw();
  30. }
  31.  
  32. static VOID PASCAL
  33. ThrowException( LPCSTR lpError0, LPCSTR lpError1, LPCSTR lpName )
  34. {
  35.   char szError[ 256 ];
  36.  
  37.   wsprintf( szError, "%s!\r\n%s [%s]", lpError0, lpError1, lpName );
  38.   TXOwl( szError ).Throw();
  39. }
  40.  
  41. static HCTL PASCAL
  42. GetNitroControl( TVbxControl *pCtl, PIUnknown p )
  43. {
  44. #ifdef __FLAT__
  45.   LPCSTR lpEXTDLL = "BDT52EXF.DLL";
  46. #else  __FLAT__
  47.   LPCSTR lpEXTDLL = "BDT52EX.DLL";
  48. #endif __FLAT__
  49.   LPFNCTLNITROFIND pCtlNitroFind;
  50.   HCTL hCtl = NULL;
  51.   HINSTANCE hEXTDLL = LoadLibrary( lpEXTDLL );
  52.  
  53.   if( hEXTDLL > HINSTANCE( HINSTANCE_ERROR ) )
  54.   {
  55.     pCtlNitroFind
  56.                =  LPFNCTLNITROFIND( GetProcAddress( hEXTDLL, "CtlNitroFind" ) );
  57.  
  58.     if( pCtlNitroFind )
  59.       hCtl = (*pCtlNitroFind)( pCtl->GetHCTL(), "BIDATASET", p );
  60.     else
  61.       ThrowException( "Finding DataSource Failed"
  62.                     , "Can't Find Dyna-Link", "CtlNitroFind()" );
  63.  
  64.     FreeLibrary( hEXTDLL );
  65.   }
  66.   else
  67.     ThrowException( "Finding DataSource Failed"
  68.                   , "Can't Load DLL", lpEXTDLL );
  69.  
  70.   return hCtl;
  71. }
  72.  
  73. static HCTL PASCAL
  74. FindNitroControl( TVbxControl *pCtl, LPCSTR lpName, TDataSource* DataSource )
  75. {
  76.   HCTL hCtl = ::GetNitroControl( pCtl, DataSource->GetPITDataSource() );
  77.  
  78.   if( hCtl )
  79.   {
  80.     LPSTR  hszName;
  81.     char szName[ 256 ];
  82.  
  83.     if( ::VBXGetProp( hCtl, 0, &hszName ) && hszName
  84.      && ::VBXGetCStringBuf( hszName, szName, sizeof( szName ) ) )
  85.     {
  86.       if( ! pCtl->SetProp( lpName, szName ) )
  87.         ThrowException( "Finding DataSource Failed"
  88.                       , "Control Has No Name", lpName );
  89.     }
  90.     else
  91.       ThrowException( "Finding DataSource Failed"
  92.                     , "Can't Get New Name", lpName );
  93.   }
  94.  
  95.   return hCtl;
  96. }
  97.  
  98. extern TDataSource* PASCAL
  99. GetSource( TVbxControl *pCtl, LPCSTR lpName, TDataSource* DataSource )
  100. {
  101.   string sName;
  102.  
  103.   if( pCtl->GetProp( lpName, sName ) )
  104.   {
  105.     HCTL hCtl = NULL;
  106.  
  107.     // first time initialization
  108.     if( ! DataSource && (DataSource = new TDataSource()) == NULL )
  109.       ThrowException( "Memory Allocation Failed in GetSource()!" );
  110.  
  111.     if( sName.length() )
  112.       hCtl = ::VBXFindControl( pCtl->Parent->HWindow, sName.c_str() );
  113.     else
  114.       hCtl = ::FindNitroControl( pCtl, lpName, DataSource );
  115.  
  116.     if( hCtl )
  117.     {
  118.       PITDataSource pitds;
  119.  
  120.       if( ! ::VBXGetPropByName( hCtl, "Object", &pitds ) )
  121.         ThrowException( "Getting DataSource Failed"
  122.                       , "Can't Get Object Property", lpName );
  123.       else if( pitds != DataSource->GetPITDataSource() )
  124.       {
  125. #ifdef __FLAT__
  126.         int  nID  = ::GetWindowLong( ::VBXGetHwnd( hCtl ), GWL_ID );
  127. #else  __FLAT__
  128.         int  nID  = ::GetWindowWord( ::VBXGetHwnd( hCtl ), GWW_ID );
  129. #endif __FLAT__
  130.         if( DataSource )
  131.           delete DataSource;
  132.         DataSource = new TDataSource( pCtl->Parent->HWindow, nID );
  133.       }
  134.     }
  135.     else
  136.     {
  137.       if( DataSource )
  138.         delete DataSource;
  139.       DataSource = new TDataSource();
  140.     }
  141.   }
  142.   else
  143.     ThrowException( "Getting DataSource Failed"
  144.                   , "Can't Get Property", lpName );
  145.  
  146.   return DataSource;
  147. }
  148.  
  149. static VOID PASCAL
  150. SetNameFromHCTL( HCTL hCtl, TVbxControl *pCtl, LPCSTR lpName )
  151. {
  152.   LPSTR  hszName;
  153.   char szName[ 256 ];
  154.  
  155.   if( ! ::VBXGetProp( hCtl, 0, &hszName ) || ! hszName )
  156.     ThrowException( "Setting DataSource Failed", "Can't Get Property", "Name" );
  157.   if( ! ::VBXGetCStringBuf( hszName, szName, sizeof( szName ) ) )
  158.     ThrowException( "Setting DataSource Failed", "Can't Get String", "Name" );
  159.   if( ! pCtl->SetProp( lpName, szName ) )
  160.     ThrowException( "Setting DataSource Failed", "Can't Set Property", lpName );
  161. }
  162.  
  163. extern TDataSource* PASCAL
  164. SetSource( TVbxControl *pCtl, LPCSTR lpName, TDataSource *DataSource )
  165. {
  166.   string sName;
  167.  
  168.   if( pCtl->GetProp( lpName, sName ) )
  169.   {
  170.     HCTL hCtl;
  171.  
  172.     if( sName.length() )
  173.     {
  174.       // Control was previously bounded to a DataAccess VBX
  175.       if( ! DataSource )
  176.       {
  177.         // Set to NULL DataSource
  178.         if( ! pCtl->SetProp( lpName, string( "" ) ) )
  179.           ThrowException( "Setting DataSource Failed"
  180.                         , "Can't Set Object to NULL", lpName );
  181.       }
  182.       else if( (hCtl = ::FindNitroControl( pCtl, lpName, DataSource )) != NULL )
  183.       {
  184.         // Switch to another DataAccess VBX with a Matching TDataSource Object
  185.         SetNameFromHCTL( hCtl, pCtl, lpName );
  186.       }
  187.       else if( (hCtl = ::VBXFindControl( pCtl->Parent->HWindow, sName.c_str() ))
  188.                                                                        != NULL )
  189.       {
  190.         // Switch the Bounded Control to the New DataSource
  191.         if( ! ::VBXSetPropByName( hCtl, "Object"
  192.                                 , LONG( DataSource->GetPITDataSource() ) ) )
  193.           ThrowException( "Setting DataSource Failed"
  194.                         , "Can't Set Property", "Object" );
  195.       }
  196.       else
  197.         ThrowException( "Setting DataSource Failed"
  198.                       , "No Such VBX Exists", lpName );
  199.     }
  200.     else if( DataSource )
  201.     {
  202.       if( (hCtl = ::FindNitroControl( pCtl, lpName, DataSource )) != NULL )
  203.       {
  204.         // Bound to a Control with the Same TDataSource
  205.         SetNameFromHCTL( hCtl, pCtl, lpName );
  206.       }
  207.       else
  208.         ThrowException( "Setting DataSource Failed"
  209.                       , "Can't Find VBX Control", "" );
  210.     }
  211.   }
  212.   else
  213.     ThrowException( "Setting DataSource Failed"
  214.                   , "Can't Get Property", lpName );
  215.  
  216.   return DataSource;
  217. }
  218.  
  219. extern TField* PASCAL
  220. GetField( TVbxControl *pCtl, int Prop_Field
  221.         , TDataSource* DataSource, TField* pField )
  222. {
  223.   // first time initialization
  224.   if( ! pField && (pField = new TField()) == NULL )
  225.     ThrowException( "Memory Allocation Failed in GetField()!" );
  226.  
  227.   if( DataSource )
  228.   {
  229.     string s;
  230.  
  231.     if( ! pCtl->GetProp( Prop_Field, s ) )
  232.       ThrowException( "Getting Field Failed!" );
  233.  
  234.     if( s.is_null() )
  235.       *pField = TField();
  236.     else
  237.       *pField = TField( DataSource->DataSet->FieldByName( s ) );
  238.   }
  239.   else
  240.     *pField = TField();
  241.  
  242.   return pField;
  243. }
  244.  
  245. extern TField* PASCAL
  246. SetField( TVbxControl *pCtl, int Prop_Field, TField *pField )
  247. {
  248.   string s;
  249.  
  250.   if( ! pCtl->SetProp( Prop_Field, pField ? *pField->FieldName : s ) )
  251.     ThrowException( "Setting Field Failed!" );
  252.  
  253.   return pField;
  254. }
  255.  
  256. extern TStrings* PASCAL
  257. GetStrings( TVbxControl *pCtl, int iIndex, TStrings* pStrings )
  258. {
  259.   HLSTR hlstrStrings;
  260.  
  261.   if( ! pCtl->GetProp( iIndex, LONG( hlstrStrings ) ) )
  262.     ThrowException( "GetStrings(): Getting HLSTR Failed!" );
  263.  
  264.   // first time initialization
  265.   if( ! pStrings && (pStrings = new TStrings()) == NULL )
  266.     ThrowException( "Memory Allocation Failed in GetStrings()!" );
  267.  
  268.   if( hlstrStrings )
  269.   {
  270.     int nStrings = ::VBXGetBasicStringLength( hlstrStrings );
  271.  
  272.     if( nStrings )
  273. #ifdef __FLAT__
  274.     {
  275.       LPSTR lpStrings = new char[ nStrings + 2 ];
  276.  
  277.       if( lpStrings )
  278.       {
  279.         int nLen
  280.               = ::VBXGetBasicStringBuf( hlstrStrings, lpStrings, nStrings + 2 );
  281.  
  282.         lpStrings[ nLen + 1 ] = '\0';
  283.         pStrings->SetText( string( lpStrings ) );
  284.         delete[] lpStrings;
  285.       }
  286.       else
  287.         ThrowException( "GetStrings(): Memory Allocation Failed!" );
  288.     }
  289.     else
  290.       pStrings->SetText( string() );
  291. #else __FLAT__
  292.       pStrings->SetText( string( ::VBXGetBasicStringPtr( hlstrStrings ) ) );
  293. #endif __FLAT__
  294.  
  295.     ::VBXDestroyBasicString( hlstrStrings );
  296.   }
  297.   else
  298.     pStrings->SetText( string() );
  299.  
  300.   return pStrings;
  301. }
  302.  
  303. extern TStrings* PASCAL
  304. SetStrings( TVbxControl *pCtl, int iIndex, TStrings* pStrings )
  305. {
  306.   HLSTR  hlstrStrings;
  307.   string sStrings;
  308.  
  309.   pStrings->GetText( sStrings );
  310.   hlstrStrings = ::VBXCreateBasicString( (void*)sStrings.c_str()
  311.                                        , short( sStrings.length() ) );
  312.   if( ! pCtl->SetProp( iIndex, LONG( hlstrStrings ) ) )
  313.     ThrowException( "SetStrings(): Setting HLSTR Failed!" );
  314.  
  315.   if( hlstrStrings )
  316.     ::VBXDestroyBasicString( hlstrStrings );
  317.  
  318.   return pStrings;
  319. }
  320.  
  321. extern string* PASCAL
  322. Getstring( TVbxControl* pCtl, int iIndex, string* String )
  323. {
  324.   // first time initialization
  325.   if( ! String && (String = new string()) == NULL )
  326.     ThrowException( "Memory Allocation Failed in GetSstring()!" );
  327.  
  328.   if( ! pCtl->GetProp( iIndex, *String ) )
  329.     ThrowException( "Getstring(): Getting string Failed!" );
  330.  
  331.   return String;
  332. }
  333.  
  334. extern string* PASCAL
  335. Setstring( TVbxControl* pCtl, int iIndex, string* String )
  336. {
  337.   if( ! pCtl->SetProp( iIndex, *String ) )
  338.     ThrowException( "Setstring(): Setting string Failed!" );
  339.  
  340.   return String;
  341. }
  342. 
  343.